# Maintainer: Chris Brannon <cmbrannon79@gmail.com>
# Contributor: Jeff Mickey <j@codemac.net>
# Contributor: Aaron, phrakture, Griffin <aaron@archlinux.org>
# Contributor: Jeffrey 'jf' Lim <jfs.world@gmail.com>

pkgname=9base
pkgver=6
pkgrel=8
pkgdesc="Port of various original Plan9 tools to unix"
url="https://tools.suckless.org/9base"
depends=(sh)
conflicts=('plan9port' '9rc-devel')
provides=('plan9')
arch=('loong64' 'x86_64')
license=('custom')
source=(https://dl.suckless.org/tools/$pkgname-$pkgver.tar.gz
        9
        plan9.sh)
md5sums=('5a4684c13fe19b00a50c2cf926d5cafc'
         'ae7108b9f26bed388e9055f35eef2986'
         '62a9e52043d9c32967fcae9018fffb56')
sha256sums=('2997480eb5b4cf3092c0896483cd2de625158bf51c501aea2dc5cf74176d6de9'
            '1da13555dc798787a73bfdfe44ded76bc007b8a0ab5fd5f8fea88cd4ae870b1e'
            'c494f86e428cf6c9d3b349e04e26e5cc3fc5c4443ec7901a1497f28939d3d4d4')

build() {
  cd $pkgname-$pkgver

  CFLAGS+=' -fcommon' # https://wiki.gentoo.org/wiki/Gcc_10_porting_notes/fno_common

  case $CARCH in
	i686) sed -i 's#^OBJTYPE\s.*$#OBJTYPE = 386#' config.mk ;;
	x86_64) sed -i 's#^OBJTYPE\s.*$#OBJTYPE = x86_64#' config.mk ;;
	esac

  sed -i 's#^PREFIX\s.*$#PREFIX = /opt/plan9#' config.mk
  sed -i 's#^CFLAGS\s*+=#CFLAGS += -DPLAN9PORT #' config.mk

  # Force dynamic linking.  Several of the programs in 9base won't work
  # when statically linked against the latest glibc.
  sed -i '/-static/d' config.mk

  make
}

package() {
  cd $pkgname-$pkgver
  make DESTDIR="$pkgdir" install
  install -m755 ../9 "$pkgdir/opt/plan9/bin/"
  install -D -m755 ../plan9.sh "$pkgdir/etc/profile.d/plan9.sh"
  install -D -m644 LICENSE "$pkgdir/usr/share/licenses/9base/LICENSE"
}
